<template>
  <view class="container">
    <uni-nav-bar
      :border="false"
      statusBar
      title="详情"
      color="#000"
      left-icon="left"
      backgroundColor="transparent"
      @clickLeft="onBack"
    >
    </uni-nav-bar>

    <view class="box">
      <view class="mainBox">
        <view class="id">{{ data?.building }}</view>
        <!-- <view class="val">{{ $util.selectDictLabel(allocateData, data?.status) }}</view> -->
        <view class="status">{{
          $util.selectDictLabel(roomStatus, data?.roomStatus)
        }}</view>
      </view>
      <view class="infoBox">
        <view class="item">
          <text class="name">楼层</text>
          <text class="val">{{ data?.floor }}</text>
        </view>
        <view class="item">
          <text class="name">房间号</text>
          <text class="val">{{ data?.roomNo }}</text>
        </view>
        <view class="item">
          <text class="name">用途</text>
          <text class="val">{{
            $util.selectDictLabel(roomFunction, data.roomFunction)
          }}</text>
        </view>
        <view class="item">
          <text class="name">类型</text>
          <text class="val">{{
            $util.selectDictLabel(roomType, data.roomType)
          }}</text>
        </view>
        <view class="item">
          <text class="name">空间</text>
          <text class="val">{{ data?.spaceNameFull }}</text>
        </view>
        <view class="item">
          <text class="name">部门</text>
          <text class="val">{{ data?.deptNames || '--' }}</text>
        </view>
      </view>
    </view>
    <view class="footer">
      <button class="btn" type="primary" @click="onAction">操 作</button>
    </view>

    <Fab :data="fabData" @itemClick="onUpdate" />

    <uni-popup ref="popup">
      <FormView :data="data" :type="type" @callback="onCallback" />
    </uni-popup>
  </view>
</template>

<script>
import {
  delRoom,
  editRoom,
  netReplace,
  netCheckIn,
  netCheckOut,
  getRoomDetail,
  netUpdateRoom,
} from '@/api/room';
import FormView from './form.vue';
import Fab from '@/components/Fab/index.vue';
import useAppStore from '@/store/modules/app';
export default {
  components: {
    Fab,
    FormView,
  },
  data() {
    return {
      roomFunction: [
        {
          label: '职工住宿',
          text: '职工住宿',
          value: '1',
        },
        {
          label: '功能',
          text: '功能',
          value: '2',
        },
        {
          label: '外来招待',
          text: '外来招待',
          value: '3',
        },
      ],
      roomType: [
        {
          label: '单人间',
          text: '单人间',
          value: '1',
        },
        {
          label: '双人间',
          text: '双人间',
          value: '2',
        },
        {
          label: '三人间',
          text: '三人间',
          value: '3',
        },
        {
          label: '四人间',
          text: '四人间',
          value: '4',
        },
        {
          label: '五人间',
          text: '五人间',
          value: '5',
        },
        {
          label: '六人间',
          text: '六人间',
          value: '6',
        },
        {
          label: '七人间',
          text: '七人间',
          value: '7',
        },
        {
          label: '夫妻间',
          text: '夫妻间',
          value: '12',
        },
        {
          label: '套间',
          text: '套间',
          value: '11',
        },
      ],
      roomStatus: [
        {
          label: '空房',
          text: '空房',
          value: '1',
        },
        {
          label: '住满',
          text: '住满',
          value: '2',
        },
        {
          label: '空余床位',
          text: '空余床位',
          value: '3',
        },
      ],
      data: {},
      query: {},
      // fab数据
      fabData: [],
      // 表单类型
      type: '',
      // store
      appStore: useAppStore(),
    };
  },
  onLoad(query) {
    this.query = query;
    this.getData();
  },
  methods: {
    getData() {
      getRoomDetail(this.query.roomId).then((res) => {
        this.data = {
          ...res.data,
          floor: this.query.floor,
          building: this.query.building,
          spaceNameFull: this.query.spaceNameFull,
        };
        // 操作项控制展示
        this.fabData = [
          {
            text: '编辑',
            icon: 'compose',
            show: true,
            color: '#4caf50',
            value: 'edit',
          },
          {
            text: '删除',
            icon: 'close',
            show: true,
            color: '#f44336',
            value: 'del',
          },
          {
            text: '入住',
            icon: 'calendar',
            show: res.data.roomFunction != 2 && res.data.roomStatus != 2,
            color: '#3f51b5',
            value: 'checkIn',
          },
          {
            text: '退房',
            icon: 'notification',
            show: res.data.roomFunction != 2 && res.data.roomStatus != 1,
            color: '#FBC02D',
            value: 'checkOut',
          },
          {
            text: '换房换铺',
            icon: 'staff',
            show: res.data.roomFunction != 2 && res.data.roomStatus != 1,
            color: '#FF5252',
            value: 'replace',
          },
          {
            text: '变更房间',
            icon: 'info',
            show: res.data.roomStatus == 1,
            color: '#009688',
            value: 'updateRoom',
          },
          {
            text: '床位历史',
            icon: 'search',
            show: true,
            color: '#9C27B0',
            value: 'bedH',
          },
          {
            text: '房务记录',
            icon: 'search',
            show: true,
            color: '#E91E63',
            value: 'bedProcess',
          },
        ];
      });
    },
    // 操作
    onAction() {
      uni.showActionSheet({
        itemList: ['床位历史', '房务记录', '床 位'],
        success: (res) => {
          if (res.tapIndex === 0) {
            this.onLocation(`/pages/room/history?roomId=${this.query.roomId}`);
          } else if (res.tapIndex === 1) {
            this.onLocation(`/pages/room/record?roomId=${this.query.roomId}`);
          } else if (res.tapIndex === 2) {
            this.onLocation(`/pages/room/bunk?roomId=${this.query.roomId}`);
          }
        },
        fail: (res) => {
          console.log(res.errMsg);
        },
      });
    },
    // 删除
    onDelete() {
      uni.showModal({
        title: '提示',
        content: '是否确认删除？',
        success: (res) => {
          if (res.confirm) {
            delRoom(this.query.roomId).then((res) => {
              uni.showToast({
                title: `删除成功`,
              });
              this.onBack();
            });
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        },
      });
    },
    // 操作项
    onUpdate(t) {
      // 删除走正常接口
      if (t === 'del') {
        this.onDelete();
        return;
      }
      // 床位历史
      if (t === 'bedH') {
        this.onLocation(`/pages/room/history?roomId=${this.query.roomId}`);
        return;
      }
      // 房务记录
      if (t === 'bedProcess') {
        this.onLocation(`/pages/room/record?roomId=${this.query.roomId}`);
        return;
      }
      // 弹窗form
      this.type = t;
      this.$refs.popup.open('bottom');
    },
    // form回调
    onCallback(r) {
      let api;
      if (this.type === 'edit') {
        api = editRoom;
      } else if (this.type === 'checkIn') {
        api = netCheckIn;
      } else if (this.type === 'checkOut') {
        api = netCheckOut;
      } else if (this.type === 'replace') {
        api = netReplace;
      } else if (this.type === 'updateRoom') {
        api = netUpdateRoom;
      }
      console.log(r);
      // loading提示
      uni.showLoading({ title: '提交中' });
      let params = {
        ...this.data,
        ...r,
      };
      api(params).then((res) => {
        uni.hideLoading();
        uni.showToast({
          title: `操作成功`,
        });
        // 刷新详情
        this.getData();
        this.$refs.popup.close();
      });
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - 160rpx);
  padding-bottom: calc(
    var(--window-bottom) + constant(safe-area-inset-bottom) + 160rpx
  );
  padding-bottom: calc(
    var(--window-bottom) + env(safe-area-inset-bottom) + 160rpx
  );
  background: linear-gradient(180deg, #d7e4fd 0%, #fff 50%);
  background-repeat: no-repeat;
  background-size: 100% auto;
  position: relative;
  .box {
    margin: 50rpx 36rpx 0;
    padding: 24rpx;
    background: #ffffff;
    border-radius: 12rpx;
    box-shadow: 0 0 30rpx 0 #ccc;
    .mainBox {
      text-align: center;
      padding: 40rpx 0;
      line-height: 1;

      .id {
        font-size: 38rpx;
        font-weight: bold;
        color: #4288ff;
        padding-bottom: 40rpx;
      }

      .val {
        font-size: 40rpx;
        font-weight: 800;
        color: #ff884d;
        padding-bottom: 27rpx;

        text {
          font-size: 30rpx;
          margin-left: 10rpx;
        }
      }

      .status {
        font-size: 26rpx;
        font-weight: 500;
        color: #555555;
      }
    }
    .infoBox {
      .item {
        font-size: 30rpx;
        font-weight: 500;
        padding: 22rpx 12rpx;
        display: flex;
        justify-content: space-between;
        border-bottom: 2rpx solid #eaeaea;
        &:last-child {
          border-bottom: 0;
        }
        .name {
          color: #989898;
        }

        .val {
          width: auto;
          color: #333;
          text-align: right;
          word-break: break-all;
        }
      }
    }
  }
  .myLabel {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .star {
      color: #dd524d;
    }
  }
  .footer {
    display: flex;
    justify-content: space-evenly;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(
      var(--window-bottom) + constant(safe-area-inset-bottom) + 60rpx
    );
    bottom: calc(var(--window-bottom) + env(safe-area-inset-bottom) + 60rpx);
    .btn {
      width: 40%;
      margin: 0;
    }
  }
  .btnBox {
    margin: 60rpx 0 10rpx;
    .btn {
      background: linear-gradient(
          to bottom,
          #d5dee7 0%,
          #e8ebf2 50%,
          #e2e7ed 100%
        ),
        linear-gradient(
          to bottom,
          rgba(0, 0, 0, 0.02) 50%,
          rgba(255, 255, 255, 0.02) 61%,
          rgba(0, 0, 0, 0.02) 73%
        ),
        linear-gradient(
          33deg,
          rgba(255, 255, 255, 0.2) 0%,
          rgba(0, 0, 0, 0.2) 100%
        );
      background-blend-mode: normal, color-burn;
    }
  }
}
</style>
